<?php
/**
 * 验证类的工厂类.
 * User: Sanit <huangsinan@zsdx.cn>
 * Date: 2017/3/6
 * Time: 20:53
 */

namespace Sanit\Validator;

use Closure;

class Factory
{
    protected $translator;

    protected $verifier;

    protected $extensions = [];

    protected $implicitExtensions = [];

    protected $replacers = [];

    protected $fallbackMessages = [];

    protected $resolver;

    public function __construct(TranslatorInterface $translator = null)
    {
        $this->translator = $translator ?: new Translator();
    }

    public function make(array $data, array $rules, array $messages = [], array $customAttributes = [])
    {
        $validator = $this->resolve($data, $rules, $messages, $customAttributes);

        if (!is_null($this->verifier)) {
            $validator->setPresenceVerifier($this->verifier);
        }

        $this->addExtensions($validator);

        return $validator;
    }

    protected function addExtensions(Validator $validator)
    {
        $validator->addExtensions($this->extensions);
        $implicit = $this->implicitExtensions;

        $validator->addImplicitExtensions($implicit);

        $validator->addReplacers($this->replacers);

        $validator->setFallbackMessages($this->fallbackMessages);
    }

    protected function resolve(array $data, array $rules, array $messages, array $customAttributes)
    {
        if (is_null($this->resolver)) {
            return new Validator($this->translator, $data, $rules, $messages, $customAttributes);
        }

        return call_user_func($this->resolver, $this->translator, $data, $rules, $messages, $customAttributes);
    }

    public function extend($rule, $extension, $message = null)
    {
        $this->extensions[$rule] = $extension;

        if ($message) {
            $this->fallbackMessages[snake_case($rule)] = $message;
        }
    }

    public function extendImplicit($rule, $extension, $message = null)
    {
        $this->implicitExtensions[$rule] = $extension;

        if ($message) {
            $this->fallbackMessages[snake_case($rule)] = $message;
        }
    }

    public function replacer($rule, $replacer)
    {
        $this->replacers[$rule] = $replacer;
    }

    public function resolver(Closure $resolver)
    {
        $this->resolver = $resolver;
    }

    public function getTranslator()
    {
        return $this->translator;
    }

    public function getPresenceVerifier()
    {
        return $this->verifier;
    }

    public function setPresenceVerifier(PresenceVerifierInterface $presenceVerifier)
    {
        $this->verifier = $presenceVerifier;
    }

}